package com.jiahe.yehua.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.yygh.model.hosp.Hospital;
import com.atguigu.yygh.vo.hosp.HospitalQueryVo;
import com.jiahe.yehua.cmn.DictFeignClient;
import com.jiahe.yehua.repository.HospitalRepository;
import com.jiahe.yehua.service.DepartmentService;
import com.jiahe.yehua.service.HospitalService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class HospitalServiceImpl implements HospitalService {
    @Autowired
    private HospitalRepository hospitalRepository;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private DictFeignClient dictFeignClient;
    /**
     * 上传医院接口到Mongondb数据库
     *
     * @param pamMap
     */
    @Override
    public void save(Map<String, Object> pamMap) {
        //把参数map集合转换对象 Hospital
        String jsonString = JSONObject.toJSONString(pamMap);
        Hospital hospital = JSONObject.parseObject(jsonString,Hospital.class);

        //判断是否村赞数据
        String hoscode = hospital.getHoscode();
        Hospital hospitalExist=  hospitalRepository.getHospitalByHoscode(hoscode);
        if (hospitalExist!=null) {

            hospital.setStatus(hospitalExist.getStatus());
            //用原来的新增时间，这里把更新时间重新修改下
            hospital.setCreateTime(hospitalExist.getCreateTime());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hospitalRepository.save(hospital);
        }else {

            //两个时间都是重新创建
            hospital.setStatus(0);
            hospital.setCreateTime(new Date());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hospitalRepository.save(hospital);
        }

    }

    /**
     * 查询医院信息
     *
     * @param hoscode
     * @return
     */
    @Override
    public Hospital getByHoscode(String hoscode) {
        Hospital hospitalByHoscode = hospitalRepository.getHospitalByHoscode(hoscode);
        return hospitalByHoscode;
    }

    /**
     * 分页查询
     *
     * @param page            当前页码
     * @param limit           每页记录数
     * @param hospitalQueryVo 查询条件
     * @return
     */
    @Override
    public Page<Hospital> selectPage(Integer page, Integer limit, HospitalQueryVo hospitalQueryVo) {
        Sort sort = Sort.by(Sort.Direction.DESC, "createTime");
        Pageable pageable=PageRequest.of(page-1, limit,sort);
        Hospital hospital=new Hospital();
        BeanUtils.copyProperties(hospitalQueryVo,hospital);
        //创建匹配器
        ExampleMatcher matcher=ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);
        Example example=Example.of(hospital,matcher);
        Page<Hospital> hospitalPage = hospitalRepository.findAll(example, pageable);

        //获取查询医院的list集合，遍历进行封装
        hospitalPage.getContent().stream().forEach(item->{
            this.setHospitalHosType(item);
        });

        return hospitalPage;
    }

    /**
     * 更新医院上线状态
     *
     * @param id
     * @param status
     */
    @Override
    public void updateStatus(String id, Integer status) {
        //查询医院信息
        Hospital hospital = hospitalRepository.findById(id).get();
        //设置修改值
        hospital.setStatus(status);
        hospital.setUpdateTime(new Date());
        hospitalRepository.save(hospital);
    }

    /**
     * 查看医院详情信息
     *
     * @param id
     * @return
     */
    @Override
    public Map<String, Object> getHospById(String id) {
        Map<String, Object> result = new HashMap<>();
        System.out.println(hospitalRepository.findById(id).get());


        Hospital hospital = this.setHospitalHosType(hospitalRepository.findById(id).get());
        //医院基本信息（包含医院等级）
        result.put("hospital",hospital);
        //单独处理更直观
        result.put("bookingRule", hospital.getBookingRule());
        //不需要重复返回
        hospital.setBookingRule(null);
        return result;
    }

    /**
     * 根据编号获取医院名称
     *
     * @param hoscode
     * @return
     */
    @Override
    public String getHospName(String hoscode) {
        Hospital hospital= hospitalRepository.getHospitalByHoscode(hoscode);
        return hospital.getHosname();
    }

    /**
     * 根据医院名称获取医院列表
     *
     * @param hosname
     */
    @Override
    public List<Hospital> findByHosname(String hosname) {
        return hospitalRepository.findHospitalByHosnameLike(hosname);
    }

    /**
     * 医院预约挂号详情
     *
     * @param hoscode
     */
    @Override
    public Map<String, Object> item(String hoscode) {
        Map<String, Object> result=new HashMap<>();
        //获取医院详情
        Hospital hospital = this.setHospitalHosType(this.getByHoscode(hoscode));
        result.put("hospital", hospital);
        //预约规则
        result.put("bookingRule", hospital.getBookingRule());
        //不需要重复返回
        hospital.setBookingRule(null);

        return result;
    }



    private Hospital setHospitalHosType(Hospital hospital) {
        //根据dictCode，和他的value获取医院等级名称
        String hostypeString = dictFeignClient.getName("Hostype", hospital.getHostype());
        //查询省 市  地区
        String provinceString = dictFeignClient.getName(hospital.getProvinceCode());
        String cityString = dictFeignClient.getName(hospital.getCityCode());
        String districtString = dictFeignClient.getName(hospital.getDistrictCode());
        //医院等级名称
        hospital.getParam().put("hostypeString",hostypeString);
        //地址，用省市区拼接
        hospital.getParam().put("fullAddress",provinceString+cityString+districtString);
        return hospital;
    }


}
